package com.dingwei.lesyndrome.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.dingwei.lesyndrome.R;
import com.dingwei.lesyndrome.bean.LoginBean;
import com.dingwei.lesyndrome.mvp.presenter.LoginPresenter;
import com.dingwei.lesyndrome.mvp.view.Contract;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.utils.KeyboardUtils;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created time 2018/2/24.
 * Creator 南柯一梦丶 on
 * Personal signature 余生还长 何必慌张
 * Page description : 登录
 */

public class LoginActivity extends BaseActivity implements Contract.LoginView {

    LoginPresenter loginPresenter = new LoginPresenter(this);
    @Bind(R.id.tv_register)
    TextView tvRegister;
    @Bind(R.id.et_account_number)
    EditText etAccountNumber;
    @Bind(R.id.et_password)
    EditText etPassword;
    @Bind(R.id.tv_forget_code)
    TextView tvForgetCode;
    @Bind(R.id.btn_login)
    SuperButton btnLogin;
    @Bind(R.id.img_wx_login)
    ImageView imgWxLogin;
    @Bind(R.id.img_qq_login)
    ImageView imgQqLogin;
    @Bind(R.id.ll_root)
    LinearLayout llRoot;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        KeyboardUtils.autoScrollView(llRoot,btnLogin);

        etAccountNumber.addTextChangedListener(textWatcher);
        etPassword.addTextChangedListener(textWatcher);

    }
    TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (!TextUtils.isEmpty(etAccountNumber.getText().toString())  && !TextUtils.isEmpty(etPassword.getText().toString())) {
                btnLogin.setShapeSolidColor(getResources().getColor(R.color.blue)).setUseShape();
                btnLogin.setEnabled(true);
            } else {
                btnLogin.setShapeSolidColor(getResources().getColor(R.color.button_no_clickable_color)).setUseShape();
                btnLogin.setEnabled(false);
            }
        }
    };

    @OnClick({R.id.tv_register, R.id.tv_forget_code, R.id.btn_login, R.id.img_wx_login, R.id.img_qq_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_register:
                intent = new Intent(BaseActivity.context, RegisterActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_forget_code:
                intent = new Intent(BaseActivity.context, ForgetPasswordActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_login:
                loginPresenter.initData(etAccountNumber.getText().toString(), etPassword.getText().toString());
                break;
            case R.id.img_wx_login:
                break;
            case R.id.img_qq_login:
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void loginSuccess(LoginBean bean) {
        BaseActivity.value.setIsLogin(true);
        BaseActivity.value.setUserName(bean.data.nickname);
        BaseActivity.value.setToken(bean.data.sessionid);
        BaseActivity.value.setPhone(bean.data.phone);
        BaseActivity.value.setHeadImg(bean.data.showHportrait);
        finish();
    }
}
